package com.blb.controller;


import com.blb.entity.Menu;
import com.blb.entity.Role;
import com.blb.entity.User;
import com.blb.service.IMenuService;
import com.blb.service.IRoleService;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qujiahao
 * @since 2019-11-20
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @Resource
    private IRoleService roleService;


    /**
     * 1.添加角色
     * 2.角色菜单关联
     * @param id
     * @return
     */
    @RequiresPermissions("rolemanage")
    @PostMapping("addrolemenu")
    @ResponseBody
    public String addrolemenu( @RequestParam(value = "ids[]") String[] id){
        int a = 1;
        String rolename = "";
        String roledescribe = "";
        Integer roleid = 0;
        for(String string: id){
            if(a==1) {
                rolename = string;
                a++;
            } else if (a==2){
                roledescribe = string;
                //增加角色
                Role role = new Role();
                role.setRoleName(rolename);
                role.setRoleDescribe(roledescribe);
                User user = ShiroUtils.getUser();
                role.setRoleCreateName(user.getUserName());
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(date);
                role.setRoleCreateDate(format);
                role.setRoleStatus("1");
                role.setCompanyId(user.getCompanyId());
                roleService.save(role);
                roleid = roleService.getIdByname(rolename, roledescribe);
                a++;
            }else {
                menuService.setrolemenu(roleid, string);
            }
        }
        return null;
    }

    /**
     * 1.修改角色
     * 2.角色菜单关联
     * @param id
     * @return
     */
    @RequiresPermissions("rolemanage")
    @PostMapping("updaterolemenu")
    @ResponseBody
    public String updaterolemenu(@RequestParam(value = "ids[]") String[] id){
        int a = 1;
        String rolename = "";
        String roledescribe = "";
        Integer roleid = 0;
        for(String string: id){
            if(a==1){
                roleid = Integer.parseInt(string);
                //先删除role——menu中roleid的所有记录
                menuService.deleteByid(roleid);
                a++;
            } else if (a==2) {
                rolename = string;
                a++;
            } else if (a==3) {
                roledescribe = string;
                //修改角色
                Role role = new Role();
                role.setId(roleid);
                role.setRoleName(rolename);
                role.setRoleDescribe(roledescribe);
                User user = ShiroUtils.getUser();
                role.setRoleUpdateName(user.getUserName());
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(date);
                role.setRoleUpdateDate(format);
                roleService.updateById(role);
                a++;
            }else {
                //储存
                menuService.setrolemenu(roleid, string);
            }
        }

        return null;
    }

}

